'use strict';

const AdminBaseController = require('../base');
const {
  listToTree
} = require("../../utils");
const {
  CommonDeletedEnum
} = require("../../utils/enum");
  
class SystemMenuController extends AdminBaseController {
  static get serviceName() {
    return 'system.systemMenu';
  }
  /**
   * 树结构
   * @returns {Promise<void>}
   */
  async tree() {
    const { ctx } = this;
    const query = this.formatQuery(ctx.query);

    // 获取所有分类列表
    const list = await this.service.list({
      ...query,
      deleted: CommonDeletedEnum.NOT_DELETED,
    });
    // 使用 dataValues 获取数据
    const plainList = list.map(item => item.get({ plain: true }));
    // 构建树形结构
    const tree = listToTree(plainList, {
      idKey: 'id',
      parentKey: 'parentId',
      childrenKey: 'children'
    });
    // 返回树形结构
    ctx.success(tree);
  }
}

module.exports = SystemMenuController;
